package com.zysf.library_boot.util;

import cn.hutool.jwt.JWTUtil;
import com.zysf.library_boot.controller.LoginController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(!(handler instanceof HandlerMethod)){
            return true;
        }
        String token = request.getHeader("Authorization");
        String uri = request.getRequestURI();
        String method = request.getMethod();
        if ("/login/userLogin".equals(uri)||"OPTIONS".equals(method)){
            return true;
        }
        //验证请求中是否有token
        if (token==null||"".equals(token)){
            throw new RuntimeException("请求头中不包含Token信息");
        }
        return JWTUtil.verify(token, LoginController.SIGNATURE.getBytes());
    }
}
